package FIT::Document::Transformer::AddBodyHeader;

use FIT::Class;
use FIT::Document::Transformer;
use FIT::Document::Parser::Twig;

extends 'FIT::Document::Transformer::Base';

has context => (
    is       => 'ro',
    isa      => 'FIT::Runner::Context',
    required => 1,
    handles  => [qw( as_summary_line is_ok )],
);

sub _transform {
    my $body  = shift;
    local $_;
    # TODO- if we want to add multiple summary tables to a doc, need to check the body
    #       header doesn't already exist before we add it
    my ($table, $row, $cell) = map { XML::Twig::Elt->new($_) } qw(table tr td);
    $table->paste( first_child => $body);
    $row->paste(   first_child => $table);
    $cell->paste(  first_child => $row);
    $table->set_att(width => '100%');
    $cell->replace_text($self->as_summary_line);
    transform { $self->is_ok? Green: Red } $cell;
}
